package com.example.serialize;

import com.example.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 * @author houyangfan
 * @version 1.0
 * @date 2022/7/28 13:25
 */
@RestController
@Slf4j
public class RedisSerializeBug {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private RedisTemplate<String, Object> userRedisTemplate;

    @Autowired
    private RedisTemplate<String, Long> longRedisTemplate;

    //@PostConstruct
    public void init() throws JsonProcessingException {
        stringRedisTemplate.opsForValue().set("stringRedisTemplate", objectMapper.writeValueAsString(new User(1, "hyf")));
        redisTemplate.opsForValue().set("redisTemplate", new User(2, "hyf"));

        log.info("redisTemplate get {}", (User)redisTemplate.opsForValue().get("redisTemplate"));
        log.info("stringRedisTemplate get {}", objectMapper.readValue(stringRedisTemplate.opsForValue().get("stringRedisTemplate"), User.class));

        log.info("===================调转=======================");
        log.info("redisTemplate get {}", redisTemplate.opsForValue().get("stringRedisTemplate"));
        log.info("stringRedisTemplate get {}", stringRedisTemplate.opsForValue().get("redisTemplate"));

    }

    @GetMapping("/redisRight")
    public void redisRight() throws JsonProcessingException {
        stringRedisTemplate.opsForValue().set("stringRedisTemplate", objectMapper.writeValueAsString(new User(1, "hyf")), 60, TimeUnit.SECONDS);
        userRedisTemplate.opsForValue().set("redisTemplate", new User(2, "hyf"),60, TimeUnit.SECONDS);
        userRedisTemplate.opsForValue().set("keyword", "侯扬帆");
        //log.info("redisTemplate get {}", (User)redisTemplate.opsForValue().get("redisTemplate"));
        //log.info("stringRedisTemplate get {}", objectMapper.readValue(stringRedisTemplate.opsForValue().get("stringRedisTemplate"), User.class));
        Object userFromRedis = userRedisTemplate.opsForValue().get("redisTemplate");
        log.info("stringRedisTemplate get {}", stringRedisTemplate.opsForValue().get("redisTemplate"));
        log.info("userredisTemplate {},{}", userFromRedis, userFromRedis.getClass());
        log.info("redisTemplate get {}", userRedisTemplate.opsForValue().get("stringRedisTemplate"));

    }

    @GetMapping("redisTestLong")
    public void redisTestLong(){
        longRedisTemplate.opsForValue().set("longNum", 3L);
        log.info(String.valueOf(longRedisTemplate.opsForValue().get("longNum")));

    }
    @GetMapping("/test")
    public void test() throws JsonProcessingException {
        log.info("color:{}", objectMapper.writeValueAsString(Color.BLUE));
    }

    /**
     * 测试 使用jackson 反序列化时，因为类的构造方法而出现的问题
     * 在该类的构造方法中，若code为2000 则返回true code 为 1234 返回false，但是反序列化的时候都返回false
     * @throws JsonProcessingException
     */
    @GetMapping("/wrong")
    public void wrong() throws JsonProcessingException {
       log.info("result:{}", objectMapper.readValue("{\"code\":1234}", ApiResultWrong.class));
       log.info("result:{}", objectMapper.readValue("{\"code\":2000}", ApiResultWrong.class));
    }

}
